<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="员工号" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入员工号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="部门名称" prop="deptName">
        <el-input
          v-model="queryParams.deptName"
          placeholder="请输入部门名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审批状态" prop="approvalStatus">
        <el-select v-model="queryParams.approvalStatus" placeholder="请选择审批状态" clearable>
          <el-option
            v-for="dict in dict.type.approval_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="审批结果" prop="approvalResult">
        <el-select v-model="queryParams.approvalResult" placeholder="请选择审批结果" clearable>
          <el-option
            v-for="dict in dict.type.approval_result"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['leave:leaveRecord:exportForTeacher']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="leaveRecordList" @selection-change="handleSelectionChange">
      <el-table-column label="请假记录ID" align="center" prop="leaveId"/>
      <el-table-column label="员工号" align="center" prop="userName"/>
      <el-table-column label="姓名" align="center" prop="nickName"/>
      <el-table-column label="部门名称" align="center" prop="deptName"/>
      <el-table-column label="请假原因" align="center" prop="leaveReason"/>
      <el-table-column label="开始时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审批人" align="center" prop="approvalName"/>
      <el-table-column label="审批状态" align="center" prop="approvalStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.approval_status" :value="scope.row.approvalStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="审批结果" align="center" prop="approvalResult">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.approval_result" :value="scope.row.approvalResult"/>
        </template>
      </el-table-column>
      <el-table-column label="审批时间" align="center" prop="approvalTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.approvalTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="批语" align="center" prop="approvalComment"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleAdd(scope.row)"
            v-hasPermi="['leave:leaveRecord:addForTeacher']"
            v-if="scope.row.approvalStatus === 0"
          >审批
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 审批请假记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="审批结果" prop="approvalResult">
          <el-select v-model="form.approvalResult" placeholder="请选择审批结果">
            <el-option
              v-for="dict in dict.type.approval_result"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="批语" prop="approvalComment">
          <el-input v-model="form.approvalComment" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listLeaveRecord,
  getLeaveRecord,
  delLeaveRecord,
  addLeaveRecord,
  updateLeaveRecord
} from "@/api/leave/leaveTeacherRecord";

export default {
  name: "LeaveTeacherRecord",
  dicts: ['approval_result', 'approval_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 请假记录表格数据
      leaveRecordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: null,
        nickName: null,
        deptName: null,
        approvalStatus: null,
        approvalResult: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        approvalResult: [
          {required: true, message: "审批结果不能为空", trigger: "blur"}
        ],
        userId: [
          {required: true, message: "用户ID不能为空", trigger: "blur"}
        ],
        leaveReason: [
          {required: true, message: "请假原因不能为空", trigger: "blur"}
        ],
        startTime: [
          {required: true, message: "开始时间不能为空", trigger: "blur"}
        ],
        endTime: [
          {required: true, message: "结束时间不能为空", trigger: "blur"}
        ],
      },
      currentLeaveId: null,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询请假记录列表 */
    getList() {
      this.loading = true;
      listLeaveRecord(this.queryParams).then(response => {
        this.leaveRecordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        leaveId: null,
        courseId: null,
        courseName: null,
        userId: null,
        userName: null,
        nickName: null,
        deptId: null,
        deptName: null,
        leaveReason: null,
        startTime: null,
        endTime: null,
        createTime: null,
        approvalId: null,
        approvalName: null,
        approvalStatus: null,
        approvalResult: null,
        approvalTime: null,
        approvalComment: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.leaveId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.currentLeaveId = row.leaveId;
      this.reset();
      this.open = true;
      this.title = "审批请假记录";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.leaveId = this.currentLeaveId;
          addLeaveRecord(this.form).then(response => {
            this.$modal.msgSuccess("请假记录批准成功");
            this.open = false;
            this.getList();
          });

        }
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('leave/leaveTeacherRecord/export', {
        ...this.queryParams
      }, `请假记录_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
